﻿using System;
using System.Windows;
using System.Windows.Navigation;
using Milkify.Interfaces;
using System.Windows.Controls;

namespace Milkify.Views
{
    /// <summary>
    /// Interaction logic for SettingsControl.xaml
    /// </summary>
    public partial class SettingsControl : PageFunction<String>, ISettingsView
    {
        bool mOKWasClicked = false;

        public SettingsControl()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(SettingsControl_Loaded);
        }

        private void SettingsControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.NavigationService != null)
                this.NavigationService.Navigating += new NavigatingCancelEventHandler(NavigationService_Navigating);
        }

        private void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            this.NavigationService.Navigating -= new NavigatingCancelEventHandler(NavigationService_Navigating);
            if (!mOKWasClicked && Cancel != null)
                Cancel(this, EventArgs.Empty);
        }

        private void ButtonOK_Click(object sender, RoutedEventArgs e)
        {
            if (Save != null)
                Save(this, EventArgs.Empty);
            mOKWasClicked = true;
            OnReturn(new ReturnEventArgs<string>());
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            OnReturn(new ReturnEventArgs<string>());
        }

        #region IView Members

        public object ViewData
        {
            get { return DataContext; }
            set { DataContext = value; }
        }

        #endregion

        #region ISettingsView Members

        public event EventHandler Save;
        public event EventHandler Cancel;

        #endregion
    }
}
